import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)
const routes = [
  {
    path: '/',
    // 重定向
    redirect: '/login'
  },
  {
    path: '/login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/home',
    component: () => import('../views/Home.vue'),
    // 重定向
    redirect: '/wellcome',
    children: [
      { path: '/wellcome', component: () => import('../views/Wellcome.vue') },
      { path: '/users', component: () => import('../views/user/Users.vue') },
      { path: '/rights', component: () => import('../views/power/Rights.vue') },
      { path: '/roles', component: () => import('../views/power/Roles.vue') },
      { path: '/categories', component: () => import('../views/goods/Cate.vue') },
      { path: '/params', component: () => import('../views/goods/Params.vue') },
      { path: '/goods', component: () => import('../views/goods/List.vue') },
      { path: '/goods/add', component: () => import('../views/goods/Add.vue') },
      { path: '/orders', component: () => import('../views/order/Orders.vue') },
      { path: '/reports', component: () => import('../views/reports/Reports.vue') }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 导航守卫解决删除token后跳转到登录页面
router.beforeEach((to, from, next) => {
  // 页面就是跳转的登录页面就直接放行
  if (to.path === '/login') return next()
  // 如果不是登录页面，查看是否有token值，如果没有就跳转到登录页面
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})
export default router
